package aszm.chahaospider;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import aszm.chahaospider.constants.Constants;
import aszm.chahaospider.constants.DBConn;
import aszm.chahaospider.parser.Fetch;

public class SpiderStarterMain {
	private static final Logger Log = Logger.getLogger(SpiderStarterMain.class.getName());
	/**
	 * 启动线程数
	 */
	private static int WORKER_NUM = 1;
	/**
	 * 抓取任务类型
	 */
	private static String TYPE = "";

	public static void main(String[] args) {
		// 初始化配置参数
		initializeParams();
		// initializeMybatis();
		// 根据type判断爬虫任务类型
		if (TYPE.equals("provice")) {
			fetchProvice();
		} else if (TYPE.equals("city")) {
			fetchCity();
		} else if (TYPE.equals("number")) {
			fetchNumber();
		} else if (TYPE.equals("shouji")) {
			fetchShouji();
		} else if (TYPE.equals("dyzz")) {
			fetchDyzz();
		} else if (TYPE.equals("sgls")) {
			fetchSglsGameActivitys();
		} else {
			Log.error("Unknown crawl type: " + TYPE + ".\n Exit...");
		}
		Log.info("程序退出");
	}

	/**
	 * 抓取shouji.911cha
	 */
	private static void fetchShouji() {
		Log.info("\n\n\n===========================\n     Fetch Shouji\n===========================\n");
		Fetch.initializefetchShouji();
	}

	/**
	 * 抓取号码
	 */
	private static void fetchNumber() {
		Log.info("\n\n\n===========================\n     Fetch Number\n===========================\n");
		Fetch.initializefetchNumber();
	}

	/**
	 * 解析城市地址
	 */
	private static void fetchCity() {
		Log.info("\n\n\n===========================\n     Fetch City\n===========================\n");
		Fetch.initializefetchCity();
	}

	/**
	 * 解析省份
	 */
	private static void fetchProvice() {
		Log.info("\n\n\n===========================\n     Fetch Provice\n===========================\n");
		Fetch.initializefetchProvice();
	}

	/**
	 * 解析电影之家
	 */
	private static void fetchDyzz() {
		Log.info("\n\n\n===========================\n     Fetch Dyzz\n===========================\n");
		Fetch.initializefetchDyzz();
	}

	/**
	 * 解析三国乱世游戏活动
	 */
	private static void fetchSglsGameActivitys() {
		Log.info("\n\n\n===========================\n     Fetch Sgls Game Activitys\n===========================\n");
		Fetch.initializefetchSglsGameActivitys();
	}

	/**
	 * 从配置文件初始化参数
	 */
	private static void initializeParams() {
		Log.info("读取配置文件……");
		InputStream in;
		try {
			in = new BufferedInputStream(new FileInputStream("conf/spider.properties"));
			Properties properties = new Properties();
			properties.load(in);

			// 从配置文件中读Mybatis路径参数
			Constants.CONFIG_PATH = properties.getProperty("DB.CONFIG_PATH");

			// 从配置文件中读取数据库连接参数
			DBConn.CONN_URL = properties.getProperty("DB.connUrl");
			DBConn.USERNAME = properties.getProperty("DB.username");
			DBConn.PASSWORD = properties.getProperty("DB.password");

			// 从配置文件中读取爬虫任务类型
			SpiderStarterMain.TYPE = properties.getProperty("spider.type");

			// 关闭配置文件读取链接
			in.close();
			Log.info("读取配置完毕！");
		} catch (FileNotFoundException e) {
			Log.error(e);
			Log.debug("配置文件没有找到");
		} catch (IOException e) {
			Log.error(e);
			Log.debug("配置文件读取异常");
		}
	}

	private static void initializeMybatis() {
		// // 映射sql的标识字符串
		// String statement =
		// "aszm.chahaospider.db.mapping.proviceMapper.getProviceFromId";
		// // 执行查询返回一个唯一provice对象的sql
		// TB_Provice provice =
		// MybatisUtils.getSqlSession().selectOne(statement, 1);
		// Log.debug(provice);

		// // 映射sql的标识字符串
		// String statement =
		// "chaohao.mybatis.dao.ProviceDtoMapper.selectByPrimaryKey";
		// // 执行查询返回一个唯一provice对象的sql
		// ProviceDto provice =
		// MybatisUtils.getSqlSession().selectOne(statement, 1);
		// Log.debug(provice.toString());

		// ProviceDto provice =
		// MybatisUtils.getSqlSession().getMapper(ProviceDtoMapper.class).selectByPrimaryKey(1);
		// Log.debug(provice.toString());

		// List<CityDto> citys =
		// MybatisUtils.getSqlSession().getMapper(CityDtoMapper.class).selectAll();
		// for (CityDto city : citys) {
		// Log.debug(city.toString());
		// }
		// List<ProviceDto> provices =
		// MybatisUtils.getSqlSession().getMapper(ProviceDtoMapper.class).selectAll();
		// for (ProviceDto provice : provices) {
		// Log.debug(provice.toString());
		// }
	}

}
